package com.bawei.home.viewmodel

import androidx.lifecycle.viewModelScope
import com.bawei.common.mvi.BaseViewModel
import com.bawei.home.HomeUiState
import com.bawei.home.HomeViewIntent
import com.bawei.home.repos.HomeRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class HomeViewModel @Inject constructor(private val repository: HomeRepository) :
    BaseViewModel<HomeViewIntent>() {
    private val _uiState = MutableStateFlow<HomeUiState>(HomeUiState.LoadEmpty)
    val uiState: StateFlow<HomeUiState> = _uiState

    override fun handIntent(uiIntent: HomeViewIntent) {
        when (uiIntent) {
            is HomeViewIntent.GetSimpleType -> loadSimpleType()
            is HomeViewIntent.LoadRecommendSimpleVideo -> loadRecommendSimpleVideo(
                uiIntent.page,
                uiIntent.pageSize
            )

            is HomeViewIntent.LoadSimpleVideoByChannelId -> loadSimpleVideoByChannelId(
                uiIntent.channelId,
                uiIntent.page,
                uiIntent.pageSize
            )

            is HomeViewIntent.LoadVideoCommends -> loadVideoCommends(
                uiIntent.dataType,
                uiIntent.itemId
            )
        }

    }

    private fun loadVideoCommends(dataType: Int, itemId: String) {
        viewModelScope.launch {
            try {
                _uiState.value = HomeUiState.LoadEmpty
                val result = repository.getVideoCommends(dataType, itemId)
                if (result.code == 0 && result.data.isNotEmpty()) {
                    _uiState.value = HomeUiState.LoadVideoCommendSuccess(result.data)
                } else {
                    _uiState.value = HomeUiState.LoadVideoCommendFail(result.msg)
                }
            } catch (e: Exception) {
                _uiState.value = HomeUiState.LoadVideoCommendFail("加载数据异常！")
            }
        }
    }

    private fun loadSimpleVideoByChannelId(channelId: String, page: Int, pageSize: Int) {
        viewModelScope.launch {
            _uiState.value = HomeUiState.LoadEmpty
            repository.getVideoByChannelId(channelId, page, pageSize)
                .catch {
                    _uiState.value = HomeUiState.LoadSimpleVideoFail("加载数据异常！")
                }
                .collect {
                    if (it.isNotEmpty()) {
                        _uiState.value = HomeUiState.LoadSimpleVideoSuccess(it)
                    } else {
                        _uiState.value = HomeUiState.LoadSimpleVideoFail("暂时没有数据！")
                    }
                }
        }
    }

    private fun loadRecommendSimpleVideo(page: Int, pageSize: Int) {
        viewModelScope.launch {
            try {
                _uiState.value = HomeUiState.LoadEmpty
                val result = repository.getRecommendVideo(page, pageSize)
                if (result.code == 0 && result.data.isNotEmpty()) {
                    _uiState.value = HomeUiState.LoadSimpleVideoSuccess(result.data)
                } else {
                    _uiState.value = HomeUiState.LoadSimpleVideoFail(result.msg)
                }
            } catch (e: Exception) {
                _uiState.value = HomeUiState.LoadSimpleVideoFail("加载数据异常！")
            }
        }
    }

    private fun loadSimpleType() {
        viewModelScope.launch {
            try {
                val result = repository.getSimpleType()
                if (result.code == 0) {
                    _uiState.value = HomeUiState.LoadSimpleTypeSuccess(result.data)
                } else {
                    _uiState.value = HomeUiState.LoadSimpleTypeFail(result.msg)
                }
            } catch (e: Exception) {
                _uiState.value = HomeUiState.LoadSimpleTypeFail("加载数据异常!")
            }
        }
    }


}